package com.swh.user.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.swh.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import com.swh.user.entity.TbAdmin;
import com.swh.user.service.TbAdminService;

import com.swh.base.entity.Result;
import com.swh.base.entity.StatusCode;

/**
 * @author swh
 * @Description 管理员 控制器层
 * @date 2021-01-15 17:27:10
 */
@RestController
@CrossOrigin

@Api(value = "tbAdmin")
@RequestMapping("/tbAdmin")
public class TbAdminController {

    @Autowired
    private TbAdminService tbAdminService;

    @Autowired
    HttpServletRequest request;

    @Autowired
    Environment env;

    /**
     * 删除
     *
     * @param id
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable String id) {
        String auth = request.getHeader("auth");
        System.out.println("auth:"+auth);
        if (auth == null) {
            return new Result(StatusCode.ACCESS_ERROR, "请先认证");
        }
        //取出前缀
        String prefix = env.getProperty("jwt.config.prefix");
        if(!auth.startsWith(prefix)){
            return new Result(StatusCode.ACCESS_ERROR, "认证有误");
        }

        // 截取token
        String token = auth.substring(prefix.length());

        Claims claims = JwtUtil.parseJWT(token);
        if(!claims.get("role").equals("admin")){
            return new Result(StatusCode.ACCESS_ERROR, "权限不足");
        }

        tbAdminService.deleteById(id);
        return new Result(StatusCode.OK, "删除成功");
    }


    /**
     * 管理员登录
     *
     * @return
     **/
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Result login(@RequestBody TbAdmin admin) {
        TbAdmin login = tbAdminService.login(admin.getLoginname(), admin.getPassword());
        if (login != null) {
            //创建token准备写到前端
            String token = JwtUtil.createJWT(admin.getId(), admin.getLoginname(), "admin");
            System.out.println(token);

            Map<String, Object> map = new HashMap<>();
            map.put("name", admin.getLoginname());
            map.put("token", token);
            return new Result(StatusCode.OK, "登录成功", map);
        }
        return new Result(StatusCode.USER_PASSWORD_ERROR, "用户名或密码错误");
    }


    /**
     * 增加用户
     *
     * @param tbAdmin
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result add(TbAdmin tbAdmin) {
        tbAdmin.setLoginname("swh");
        tbAdmin.setPassword("admin");
        tbAdmin.setState("0");
        tbAdminService.add(tbAdmin);
        return new Result(StatusCode.OK, "注册成功");
    }


    /**
     * 查询全部数据
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll() {
        return new Result(StatusCode.OK, "查询成功", tbAdminService.findAll());
    }

    /**
     * 根据ID查询
     *
     * @param id ID
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result findById(@PathVariable String id) {
        return new Result(StatusCode.OK, "查询成功", tbAdminService.findById(id));
    }

    /**
     * 分页+多条件查询
     *
     * @param searchMap 查询条件封装
     * @param page      页码
     * @param size      页大小
     * @return 分页结果
     */
    @RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.POST)
    public Result findSearch(@RequestParam Map searchMap, @PathVariable int page, @PathVariable int size) {
        Page<TbAdmin> pageList = tbAdminService.findSearch(searchMap, page, size);
        return new Result(StatusCode.OK, "查询成功", pageList);
    }

    /**
     * 根据条件查询
     *
     * @param searchMap
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public Result findSearch(@RequestParam Map searchMap) {
        return new Result(StatusCode.OK, "查询成功", tbAdminService.findSearch(searchMap));
    }


    /**
     * 修改
     *
     * @param tbAdmin
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public Result update(TbAdmin tbAdmin, @PathVariable String id) {
        tbAdmin.setId(id);
        tbAdminService.update(tbAdmin);
        return new Result(StatusCode.OK, "修改成功");
    }


}
